#!/bin/sh

if [ "$0" != "./build-scripts/build-auth-semistatic" ]; then
  echo "Please run me from the root checkout dir"
  exit 1
fi

if [ -z "$VERSION" ]; then
  echo 'Please set $VERSION' >&2
  exit 1
fi

if [ -z "$RELEASE" ];then
  echo 'Please set $RELEASE' >&2
  exit 1
fi

if [ -z "$DISTDIR" ]; then
  DISTDIR=pdns
fi

set -e
set -x

rm -rf $DISTDIR/debian/
cp -r build-scripts/debian-authoritative-semistatic $DISTDIR/debian

cd $DISTDIR

cat > debian/changelog << EOF
pdns (${VERSION}-${RELEASE}) unstable; urgency=medium

  * Automatic build

 -- PowerDNS.COM AutoBuilder <noreply@powerdns.com>  $(date -R)

EOF

fakeroot debian/rules binary-static

cat > pdns.spec <<EOF
BuildRoot: /tmp/pdns
Name: pdns-static
Version: $VERSION
Release: $RELEASE
Summary: extremely powerful and versatile nameserver
License: GPLv2
Distribution: Neutral
Vendor: PowerDNS.COM BV
Group: System/DNS

%define _rpmdir ../
%define _rpmfilename %%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm

%description
PowerDNS is a versatile nameserver which supports a large number
of different backends ranging from simple zonefiles to relational
databases and load balancing/failover algorithms.

This RPM is statically compiled and should work on all Linux distributions.
It comes with support for MySQL, PostgreSQL, Bind zonefiles and the 'pipe
backend'.

%files
%defattr(-,root,root)
%{_sbindir}/pdns_server
%{_bindir}/pdns_control
%{_bindir}/zone2sql
%{_bindir}/zone2json
%{_bindir}/pdnssec
%{_mandir}/man1/pdns_control.1
%{_mandir}/man1/pdns_server.1
%{_mandir}/man1/zone2sql.1
%{_mandir}/man1/pdnssec.1
%{_datadir}/doc/pdns/*.sql

%dir %{_sysconfdir}/powerdns/
%config(noreplace) %{_sysconfdir}/powerdns/pdns.conf
%config %{_sysconfdir}/init.d/pdns

%post
echo Remember to create a 'pdns' user before starting pdns

%package -n pdns-tools
Summary: extremely powerful and versatile nameserver
License: GPLv2
Distribution: Neutral
Vendor: PowerDNS.COM BV
Group: System/DNS

%description -n pdns-tools
These are the tools

%files -n pdns-tools
%defattr(-,root,root)
%{_bindir}/calidns
%{_bindir}/dnsbulktest
%{_bindir}/dnsgram
%{_bindir}/dnsreplay
%{_bindir}/dnsscan
%{_bindir}/dnsscope
%{_bindir}/dnstcpbench
%{_bindir}/dnswasher
%{_bindir}/dumresp
%{_bindir}/notify
%{_bindir}/nproxy
%{_bindir}/nsec3dig
%{_bindir}/saxfr
%{_mandir}/man1/dnsbulktest.1
%{_mandir}/man1/dnsgram.1
%{_mandir}/man1/dnsreplay.1
%{_mandir}/man1/dnsscan.1
%{_mandir}/man1/dnsscope.1
%{_mandir}/man1/dnstcpbench.1
%{_mandir}/man1/dnswasher.1
%{_mandir}/man1/nsec3dig.1
%{_mandir}/man1/saxfr.1

EOF

BUILDROOT=$(pwd)/rpm-buildroot/
fakeroot /bin/sh -c "rm -rf $BUILDROOT ; DESTDIR=$BUILDROOT make install-strip"
fakeroot rm -f $BUILDROOT/usr/lib/pdns/*.a $BUILDROOT/usr/lib/pdns/*.la $BUILDROOT/usr/lib/*.a $BUILDROOT/usr/lib/*.la
fakeroot mkdir -p $BUILDROOT/etc/init.d
fakeroot cp pdns/pdns.init $BUILDROOT/etc/init.d/pdns || fakeroot cp pdns/pdns $BUILDROOT/etc/init.d/pdns
fakeroot mkdir -p $BUILDROOT/etc/powerdns
fakeroot mv $BUILDROOT/etc/powerdns/pdns.conf-dist $BUILDROOT/etc/powerdns/pdns.conf
fakeroot rm -f $BUILDROOT/usr/bin/zone2ldap $BUILDROOT/usr/man/man1/zone2ldap.1 $BUILDROOT/usr/man/man1/zone2ldap.1 $BUILDROOT/usr/share/man/man1/zone2ldap.1 $BUILDROOT/usr/share/man/man1/zone2ldap.1
fakeroot rpmbuild -bb pdns.spec --buildroot $BUILDROOT
